package com.fanfan.service.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fanfan.constant.SystemConstants;
import com.fanfan.dao.PetDao;
import com.fanfan.enums.AppHttpCodeEnum;
import com.fanfan.exception.SystemException;
import com.fanfan.pojo.Article;
import com.fanfan.pojo.Category;
import com.fanfan.pojo.Pet;
import com.fanfan.pojo.domain.ResponseResult;
import com.fanfan.pojo.vo.ArticleListVo;
import com.fanfan.pojo.vo.PageVo;
import com.fanfan.pojo.vo.PetVo;
import com.fanfan.service.PetService;
import com.fanfan.service.UserService;
import com.fanfan.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 用户表(Pet)表服务实现类
 *
 * @author fansea
 * @since 2023-03-09 22:30:57
 */
@Service("petService")
public class PetServiceImpl extends ServiceImpl<PetDao, Pet> implements PetService {
    @Autowired
    UserService userService;

    @Override
    public ResponseResult petList(Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<Pet> queryWrapper=new LambdaQueryWrapper();
        queryWrapper.eq(Pet::getStatus,SystemConstants.ARTICLE_STATUS_NORMAL);
        //分页查询
        Page<Pet> page = new Page<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        page(page,queryWrapper);

        List<Pet> petList = page.getRecords();
        PageVo pageVo = new PageVo(petList,page.getTotal());
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult peDetail(Long petId) {
        Pet pet = getBaseMapper().selectById(petId);
        PetVo petVo = BeanCopyUtils.copyOne(pet, PetVo.class);
        petVo.setNickName(userService.getById(petVo.getCreateBy()).getNickName());
        return ResponseResult.okResult(petVo);
    }

    @Override
    public ResponseResult petAdd(Pet pet) {
        if (pet.getAvatar()!=null&&pet.getPetName()!=null){
            save(pet);
            return ResponseResult.okResult();
        }throw new SystemException(AppHttpCodeEnum.ADD_ERROR);


    }

    @Override
    public ResponseResult petList(Integer pageNum, Integer pageSize, Pet pet) {
        LambdaQueryWrapper<Pet> queryWrapper=new LambdaQueryWrapper();
        queryWrapper.eq(Objects.nonNull(pet.getPetName()),Pet::getPetName,pet.getPetName());
        queryWrapper.eq(Objects.nonNull(pet.getPosition()),Pet::getPosition,pet.getPosition());
        //分页查询
        Page<Pet> page = new Page<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        page(page,queryWrapper);

        List<Pet> petList = page.getRecords();
        PageVo pageVo = new PageVo(petList,page.getTotal());
        return ResponseResult.okResult(pageVo);
    }
}

